package com.ymx.darling.logicdelete;

import com.ymx.darling.bean.entity.FieldInfo;
import com.ymx.darling.wrapper.UpdateWrapper;
import com.ymx.darling.wrapper.Wrapper;
import com.ymx.darling.wrapper.fragment.WhereSegment;

import java.util.function.Consumer;

/**
 * 逻辑删除处理器
 * @author 爱Java的小于
 */
public abstract class LogicDeleteProcessor {
    public void setWrapperCondition(String tableName, FieldInfo logicDeleteFieldInfo, Wrapper<?> wrapper) {
        String column = logicDeleteFieldInfo.getColumn();
        String conditionName = String.format("%s.%s", tableName, column);
        Object conditionValue = this.getNormalValue();
        wrapper.eq(conditionName, conditionValue);
    }

    public void setWhereCondition(String tableName, FieldInfo logicDeleteFieldInfo, WhereSegment whereSegment) {
        String column = logicDeleteFieldInfo.getColumn();
        String conditionName = String.format("%s.%s", tableName, column);
        Object conditionValue = this.getNormalValue();
        whereSegment.eq(conditionName, conditionValue);
    }

    public void addUpdateWrapperSetValue(FieldInfo logicDeleteFieldInfo, UpdateWrapper updateWrapper) {
        String column = logicDeleteFieldInfo.getColumn();
        updateWrapper.set(column, this.getDeletedValue());
        // 调用后置方法处理用户自定义操作
        Consumer<UpdateWrapper> afterSetLogicDeleteProcessor = LogicDeleteManager.getAfterSetLogicDeleteProcessor();
        if (afterSetLogicDeleteProcessor != null) {
            afterSetLogicDeleteProcessor.accept(updateWrapper);
        }
    }

    /**
     * 逻辑删除字段的默认值
     * @return Object
     */
    public abstract Object getNormalValue();

    /**
     * 逻辑删除字段的删除值
     * @return Object
     */
    public abstract Object getDeletedValue();
}
